package club.itstudio.itouc.controller.admin

import club.itstudio.itouc.config.AdminConfig
import club.itstudio.itouc.core.annotation.AdminRequired
import club.itstudio.itouc.core.util.Result
import club.itstudio.itouc.core.util.ResultUtil
import club.itstudio.itouc.util.Redis
import org.springframework.web.bind.annotation.*

/**
 * 管理员成员管理
 */
@RestController
@RequestMapping("/admin/manager")
class AdminManager (val adminConfig: AdminConfig,
                    val redis: Redis){

    // 获取管理员
    @AdminRequired
    @GetMapping
    fun getAdmin() = ResultUtil.success(adminConfig.adminSet)

    // 添加管理员
    @AdminRequired
    @PostMapping
    fun addAdmin(@RequestParam userCode: String): Result<Any> {
        adminConfig.adminSet.add(userCode)
        adminConfig.timeStamp = System.currentTimeMillis()
        redis.saveAdminConfig(adminConfig)
        return ResultUtil.success()
    }

    // 删除管理员
    @AdminRequired
    @DeleteMapping
    fun delAdmin(@RequestParam userCode: String): Result<Any> {
        adminConfig.adminSet.remove(userCode)
        adminConfig.timeStamp = System.currentTimeMillis()
        redis.saveAdminConfig(adminConfig)
        return ResultUtil.success()
    }
}